package ui;

import model.Student;

import javax.swing.*;
import java.awt.*;

public class StudentMainFrame extends JFrame {

    private final Student currentStudent;

    private JPanel cardPanel;
    private CardLayout cardLayout;

    public StudentMainFrame(Student student) {
        this.currentStudent = student;

        setTitle("奖学金申请系统 - 学生主页");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 顶部按钮面板
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));

        JButton btnProfile = new JButton("个人信息");
        JButton btnApply = new JButton("奖学金申请");
        JButton btnRecords = new JButton("申报记录");
        JButton btnLogout = new JButton("退出登录"); // 新增退出登录按钮

        UIUtil.stylePinkButton(btnProfile);
        UIUtil.stylePinkButton(btnApply);
        UIUtil.stylePinkButton(btnRecords);
        UIUtil.stylePinkButton(btnLogout);

        topPanel.add(btnProfile);
        topPanel.add(btnApply);
        topPanel.add(btnRecords);
        topPanel.add(btnLogout); // 添加退出登录按钮到顶部面板

        add(topPanel, BorderLayout.NORTH);

        // 卡片布局面板
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);

        // 三个面板
        cardPanel.add(createProfilePanel(), "profile");
        cardPanel.add(new ScholarshipApplyPanel(currentStudent), "apply");
        ApplicationRecordPanel recordPanel = new ApplicationRecordPanel(currentStudent);
        recordPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
        cardPanel.add(recordPanel, "record");

        add(cardPanel, BorderLayout.CENTER);

        // 默认显示个人信息
        cardLayout.show(cardPanel, "profile");

        // 按钮切换事件
        btnProfile.addActionListener(e -> cardLayout.show(cardPanel, "profile"));
        btnApply.addActionListener(e -> cardLayout.show(cardPanel, "apply"));
        btnRecords.addActionListener(e -> cardLayout.show(cardPanel, "record"));

        // 退出登录按钮事件：关闭当前窗口，回到身份选择界面
        btnLogout.addActionListener(e -> {
            dispose();  // 关闭学生主页窗口
            Main.showMainMenu();  // 调用主界面显示身份选择
        });

        setVisible(true);
    }

    private JPanel createProfilePanel() {
        JPanel outerPanel = new JPanel();
        outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));
        outerPanel.setBackground(Color.WHITE);

        JLabel titleLabel = new JLabel("个人信息");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setForeground(new Color(255, 105, 180));
        titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        titleLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));

        JPanel contentPanel = new JPanel();
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.X_AXIS));
        contentPanel.setBackground(Color.WHITE);
        contentPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(255, 182, 193), 3),
                BorderFactory.createEmptyBorder(20, 20, 20, 20)
        ));

        JLabel avatarLabel = new JLabel();
        ImageIcon icon = new ImageIcon("C:\\Users\\Wangyongqian\\Pictures\\Camera Roll\\b_e623865f848249a5b5d5de6c61f2d07a.jpg");
        Image img = icon.getImage().getScaledInstance(120, 150, Image.SCALE_SMOOTH);
        avatarLabel.setIcon(new ImageIcon(img));
        avatarLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20));

        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
        infoPanel.setBackground(Color.WHITE);
        infoPanel.add(createDataLabel("学号：" + currentStudent.getStudentId()));
        infoPanel.add(Box.createVerticalStrut(8));
        infoPanel.add(createDataLabel("姓名：" + currentStudent.getName()));
        infoPanel.add(Box.createVerticalStrut(8));
        infoPanel.add(createDataLabel("专业：" + currentStudent.getMajor()));
        infoPanel.add(Box.createVerticalStrut(8));
        infoPanel.add(createDataLabel("年级：" + currentStudent.getGrade()));

        contentPanel.add(avatarLabel);
        contentPanel.add(infoPanel);

        outerPanel.add(titleLabel);
        outerPanel.add(Box.createVerticalStrut(10));
        outerPanel.add(contentPanel);

        return outerPanel;
    }

    private JLabel createDataLabel(String text) {
        JLabel label = new JLabel(text);
        label.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        label.setForeground(Color.BLACK);
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(BorderFactory.createEmptyBorder(0, 60, 0, 0));
        label.setAlignmentX(Component.LEFT_ALIGNMENT);
        return label;
    }
}
